﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@

@{
    //Layout = "~/Views/Shared/_FormLayout.cshtml";
    Layout = "~/Views/Shared/__ComponentLayout.cshtml";
    string editId = ViewData.Count > 0 ? ViewData["0"].ToString() : "null";
}
@using ST.Torch.WebApi.Web.Core;
@inherits UtilRazorPage<object>
<div style="padding: 16px;"></div>

<form style="padding: 16px;padding-right:30px" class="layui-form" action="" lay-filter="edit-data">
    <input type="text" name="Id" placeholder="" autocomplete="off" class="layui-input" style="display:none">
    <div class="layui-form-item" id="checkboxEle">
     @*    <input type="checkbox" name="A1" value="1" title="默认" lay-skin="tag">
        <input type="checkbox" name="A2" value="2" title="选中" lay-skin="tag">
        <input type="checkbox" name="A3" value="3" title="禁用" lay-skin="tag"> *@
    </div>




    <div class="layui-form-item FormSubmit" style="text-align:right">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">@L("立即提交")</button>
            <button type="button" class="layui-btn layui-btn-primary" lay-filter="Close">@L("关闭")</button>
        </div>
    </div>
</form>
@section footer
{
    <script>
        layui.use(['form', 'toast', 'tree'], function () {
            var form = layui.form;
            var toast = layui.toast;
            var tree = layui.tree;
            //var editId = '@GetValue("Id", "null")';
            var editId = '@editId';
            var initDate = function (Data) {
                var htmlStr = "";
                for (var i in Data) {
                    var tempData = Data[i];
                    var tempmiddle = ""
                    if (tempData.checked) {
                        tempmiddle = "checked"
                    }
                    htmlStr += `<input type="checkbox" name="A` + tempData.id + `" value="` + tempData.id + `" title="` + tempData.title + `"  ` + tempmiddle + `  lay-skin="tag">`
                }
                $("#checkboxEle").html(htmlStr);
                form.render();
            }

            Query();

            //监听按钮
            $(".layui-form button").click(function () {
                var id = $(this).attr('lay-filter');
                //alert(id);
                switch (id) {
                    case "Close": closeOpen(); break;
                }
            });
            var buttonEle = null;
            //监听提交
            form.on('submit(formDemo)', function (data) {
                console.log("--submit--")
                buttonEle = button.load({ elem: this });

                console.log(data.field)
                var Roles = [];
                for (var i in data.field) {
                    console.log(i)
                    if (i == "Id") continue
                    Roles.push(data.field[i])
                }
                Add(Roles);
                return false;
            });

            function Query() {
                var formData = {
                    Id: editId
                };
                $.ajax({
                    type: 'POST',
                    url: '/api/SYSUser/GetUserRole',//发送请求
                    data: formData,
                    success: function (result) {
                        console.log(result)
                        if (result.Succeeded) {
                            initDate(result.Data)
                        } else {
                            toast.error({
                                message: '@L("查询失败")',
                            });

                        }
                    }
                });
            }
            function Add(data) {
                var tempData = {
                    UserId: editId,
                    RoleIds: data,
                }
                $.ajax({
                    type: 'POST',
                    url: '/api/SYSUser/InsertUserRole',//发送请求
                    data: tempData,
                    success: function (result) {
                        console.log(result)
                        buttonEle.stop();

                        if (result.Succeeded) {
                            toast.success({
                                message: '@L("授权成功")',
                            });
                            closeOpen();
                        } else {
                            var msgError = '@L("授权失败")';
                            if (result.Errors != "") {
                                msgError = result.Errors
                            }
                            toast.error({
                                message: msgError,
                            });
                        }
                    }, error: function (err) {
                        buttonEle.stop();
                    }
                });
            }

            function closeOpen() {

                parent.layer.close(G_LayuiOpenTypeIndex);
                return true;
            }

        });
    </script>
}